package com.ecoolex.chinadutyfree.retailer.core.manager;

import java.sql.Date;
import java.time.Instant;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ecoolex.chinadutyfree.retailer.core.dao.DirectMailGoodsDeliveryMannerDAO;
import com.ecoolex.chinadutyfree.retailer.entity.DirectMailGoods;
import com.ecoolex.chinadutyfree.retailer.entity.DirectMailGoodsDeliveryManner;
import com.ecoolex.chinadutyfree.retailer.entity.DirectMailGoodsDeliveryManner.Column;
import com.ecoolex.chinadutyfree.retailer.entity.DirectMailGoodsDeliveryMannerExample;
import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.util.BizAssert;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.framework.common.util.CollectorUtil;

@Service
public class DirectMailGoodsDeliveryMannerManager {

	@Autowired
	private DirectMailGoodsDeliveryMannerDAO dao;

	@Autowired
	private DeliveryMannerManager deliveryMannerManager;

	public boolean isSupport(Integer directMailGoodsId, Integer deliveryMannerId) {
		DirectMailGoodsDeliveryMannerExample example = new DirectMailGoodsDeliveryMannerExample();
		example.createCriteria().andDirectMailGoodsIdEqualTo(directMailGoodsId).andDeliveryMannerIdEqualTo(
			deliveryMannerId).andDeletedEqualTo(DirectMailGoodsDeliveryManner.NOT_DELETED);
		return dao.countByExample(example) > 0;
	}

	public Long countByDeliveryMannerId(Integer deliveryMannerId) {
		DirectMailGoodsDeliveryMannerExample example = new DirectMailGoodsDeliveryMannerExample();
		example.createCriteria().andDeliveryMannerIdEqualTo(deliveryMannerId).andDeletedEqualTo(
			DirectMailGoodsDeliveryManner.NOT_DELETED);
		return dao.countByExample(example);
	}

	private void verify(Integer deliveryMannerId) {
		BizAssert.isTrue(Check.notNull(deliveryMannerId), ResultCode.VERIFY.build(20), "deliveryMannerId为空");
		BizAssert.isTrue(Check.notNull(deliveryMannerManager.getById(deliveryMannerId)),
			ResultCode.VERIFY.build(21),
			"DeliveryManner为空");
	}

	public void update(DirectMailGoods goods, List<Integer> deliveryMannerList, String updateUser) {
		List<DirectMailGoodsDeliveryManner> oldList = queryByDirectMailGoodsId(goods.getDirectMailGoodsId());

		deliveryMannerList.forEach(deliveryMannerId -> {
			verify(deliveryMannerId);
			DirectMailGoodsDeliveryManner old
				= oldList.stream().collect(CollectorUtil.first(d -> d.getDeliveryMannerId().equals(deliveryMannerId)));
			if (Check.notNull(old)) {
				oldList.remove(old);
			} else {
				DirectMailGoodsDeliveryManner record = new DirectMailGoodsDeliveryManner();
				record.setDirectMailGoodsId(goods.getDirectMailGoodsId());
				record.setDeliveryMannerId(deliveryMannerId);
				record.setDeleted(DirectMailGoodsDeliveryManner.NOT_DELETED);
				record.setCreateUser(updateUser);
				record.setCreateTime(Date.from(Instant.now()));
				record.setUpdateUser(updateUser);
				record.setUpdateTime(Date.from(Instant.now()));
				dao.insert(record);
			}
		});

		// 删除已有的
		oldList.forEach(old -> delete(old, updateUser));
	}

	private void delete(DirectMailGoodsDeliveryManner record, String updateUser) {
		record.setDeleted(DirectMailGoodsDeliveryManner.IS_DELETED);
		record.setUpdateUser(updateUser);
		record.setUpdateTime(Date.from(Instant.now()));
		dao.updateByPrimaryKeySelective(record, Column.deleted, Column.updateUser, Column.updateTime);
	}

	public void deleteByDirectMailGoodsId(Integer directMailGoodsId, String updateUser) {
		queryByDirectMailGoodsId(directMailGoodsId).forEach(record -> delete(record, updateUser));
	}

	public List<DirectMailGoodsDeliveryManner> queryByDirectMailGoodsId(Integer directMailGoodsId) {
		DirectMailGoodsDeliveryMannerExample example = new DirectMailGoodsDeliveryMannerExample();
		example.createCriteria().andDirectMailGoodsIdEqualTo(directMailGoodsId).andDeletedEqualTo(
			DirectMailGoodsDeliveryManner.NOT_DELETED);
		example.orderBy(Column.deliveryMannerId.asc());
		return dao.selectByExample(example);
	}
}